#include "AstroidSave.h"

#include "AppliedFieldVector3.h"

#include <cmath>
#include <QFile>
#include <QTextStream>
#include <cstdlib>


AstroidSave::AstroidSave()
{

}

AstroidSave::~AstroidSave()
{

}

int AstroidSave::initialize(Output output)
{
  //copy Parameters file
  QString command="cp ../Parameters.cc ";
  command+=output.getParamsFile();
  system(command.toAscii().data());
  QFile myfile(output.getFileName());
  QTextStream stream(&myfile);
  myfile.open(QIODevice::Append);
  stream<<"Angle\t";
  stream<<"hx\t";
  stream<<"hy\t";
  stream<<"hz\t";
  stream<<"mx\t";
  stream<<"my\t";
  stream<<"mz\n";
  myfile.close();    
  return 0;

}

int AstroidSave::save(Clock clock,Output output,Pulse *field,Layer mag)
{
 
  QFile myfile(output.getFileName());
  QTextStream stream(&myfile);
  myfile.open(QIODevice::Append);
  stream<<field->getAngle()<<"\t";
  Vector3 happ;
  happ=AppliedFieldVector3(clock,field);
  stream<<happ.getX()<<"\t";
  stream<<happ.getY()<<"\t";
  stream<<happ.getZ()<<"\t";
  /* myfile<<field->getLevel(clock.getTimeStamp())*sin(field->getAngle()*M_PI/180)*cos(field->getAngle2()*M_PI/180)<<"\t";
  myfile<<field->getLevel(clock.getTimeStamp())*sin(field->getAngle2()*M_PI/180)*sin(field->getAngle()*M_PI/180)<<"\t";
  myfile<<field->getLevel(clock.getTimeStamp())*cos(field->getAngle()*M_PI/180)<<"\t";*/
  happ=mag.getMag();
  stream<<happ.getX()<<"\t";
  stream<<happ.getY()<<"\t";
  stream<<happ.getZ()<<"\t";
  stream<<"\n";
  myfile.close();    
  return 0;

}
